ext.define('extension.view', function() {

var messages = extension.messages;
var utils = extension.utils;
var gui = extension.gui;
var Sidebar = extension.gui.Sidebar;
var Dialog = extension.gui.Dialog;
var Highlighter = extension.gui.Highlighter;
var dialog_callback;

function onSidebarClick(offset) {
    gui.scrollTo(offset, function() {
        messages.send('view-page-scrolled', offset);
    });
}

function onDialogHighlight() {
    var settings = Dialog.getInstance().getSettings();
    startHighlight({
        color: settings.color,
        callback: function(rectangle) {
            settings.box = rectangle;
            dialog_callback('highlight', settings);
        }
    });
}

function onDialogSave() {
    var settings = Dialog.getInstance().getSettings();
    dialog_callback('save', settings);
}

function onDialogCancel() {
    dialog_callback('cancel');
}

function updateTag(tag) {
    var sidebar = Sidebar.getInstance();
    var element = sidebar.searchNote(tag.offset);
    var content = element.querySelector('.content');
    content.classList.remove(utils.extractColor(content.className));
    content.classList.add(tag.color);
    var body = element.querySelector('.body');
    body.innerText = tag.text;
    sidebar.show();
}

function markPage(tag) {
    var sidebar = Sidebar.getInstance();
    sidebar.addNote(tag);
    sidebar.show();
}

function unmarkPage(tag) {
    var sidebar = Sidebar.getInstance();
    var note = sidebar.searchNote(tag.offset);
    sidebar.removeNote(note);
    hideEmptySidebar();
}

function hideEmptySidebar() {
    var sidebar = Sidebar.getInstance();
    if (!sidebar.hasNotes())
        sidebar.hide();
}

function lockSidebar() {
    Sidebar.getInstance().lock(true);
}

function unlockSidebar() {
    Sidebar.getInstance().lock(false);
}

function toggleSidebar() {
    var sidebar = Sidebar.getInstance();
    if (sidebar != null)
        sidebar.isOpen() ? sidebar.hide() : sidebar.show();
}

function collapseNotes() {
    Sidebar.getInstance().collapseNotes();
}

function openDialog(config) {
    var dialog = Dialog.getInstance();
    if (config) {
        dialog_callback = config.callback || utils.nop();
        dialog.setTitle(config.title);
        dialog.setText(config.text);
        dialog.selectColor(config.color);
        dialog.setPosition({right: 0, bottom: 0});
    }
    dialog.open();
}

function closeDialog() {
    dialog_callback = null;
    Dialog.getInstance().close();
}

function highlightPage(config) {
    Highlighter.getInstance().pulse(config);
}

function startHighlight(config) {
    collapseNotes();
    Dialog.getInstance().close();
    Highlighter.getInstance().start({
        color: utils.mapColor(config.color),
        callback: function(rectangle) {
            stopHighlight();
            if (rectangle)
                config.callback(rectangle);
        }
    });
}

function stopHighlight() {
    var highlighter = Highlighter.getInstance();
    if (highlighter.isOpen()) {
        highlighter.stop();
        unlockSidebar();
    }
}

function toggleHighlighter(config) {
    if (Highlighter.getInstance().isOpen())
        stopHighlight();
    else
        startHighlight(config);
}

return {
    bind: function() {
        messages.listen({
            'model-tag-added': markPage,
            'model-tag-removed': unmarkPage,
            'model-tag-updated': updateTag,
            'view-dialog-highlight': onDialogHighlight,
            'view-dialog-save': onDialogSave,
            'view-dialog-cancel': onDialogCancel,
            'view-sidebar-click': onSidebarClick
        });
    },
    lockSidebar: lockSidebar,
    unlockSidebar: unlockSidebar,
    toggleSidebar: toggleSidebar,
    collapseNotes: collapseNotes,
    openDialog: openDialog,
    closeDialog: closeDialog,
    highlightPage: highlightPage,
    showHighlighter: toggleHighlighter
};

});
